package com.mayikt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.dto.req.UserLoginDTO;
import com.mayikt.entity.UserDO;
import com.mayikt.manage.AsyncLoginLogManage;
import com.mayikt.mapper.UserMapper;
import com.mayikt.service.MemberLoginService;
import com.mayikt.utils.ChannelUtils;
import com.mayikt.utils.MD5Util;
import com.mayikt.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

/**
 * @Name MemberLoginServiceImpl
 * @Author 枫火
 * @Date 2021/5/5 15:29
 * @Description
 **/

@RestController
@Slf4j
//@CrossOrigin
public class MemberLoginServiceImpl extends BaseApiService implements MemberLoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenUtil tokenUtil;

    @Value("${mayikt.login.token.prefix}")
    private String loginTokenPrefix;

    @Autowired
    private AsyncLoginLogManage asyncLoginLogManage;

    @Autowired
    private ChannelUtils channelUtils;

    @Override
    public BaseResponse<JSONObject> login(UserLoginDTO userLoginDto, String sourceIp, String channel, String deviceInfor) throws UnsupportedEncodingException {
        // 参数验证
        String mobile = userLoginDto.getMobile();
        if (StringUtils.isEmpty(mobile)) {
            return setResultError("mobile参数不能为空");
        }
        String password = userLoginDto.getPassword();
        if (StringUtils.isEmpty(password)) {
            return setResultError("password参数不能为空");
        }
        // 查询数据库
        String newPassword = MD5Util.MD5(password);
        UserDO loginUserDo = userMapper.login(mobile, newPassword);
        if (loginUserDo == null) {
            return setResultError("手机号码或者密码不正确");
        }
        // 设备信息
        if (StringUtils.isEmpty(deviceInfor)) {
            return setResultError("设备信息不能为空");
        }
        // 如果传中文，请求参数URLEncode处理后再传参
        deviceInfor = URLDecoder.decode(deviceInfor, "UTF-8");
        if (!channelUtils.existChannel(channel)) {
            return setResultError("渠道来源错误");
        }
        Long userId = loginUserDo.getUserId();
        String userToken = tokenUtil.createToken(loginTokenPrefix, userId + "");
        JSONObject resultJSON = new JSONObject();
        resultJSON.put("userToken", userToken);
        String wxOpenId = loginUserDo.getWxOpenId();
        String openIdToken = userLoginDto.getOpenIdToken();
        asyncLoginLogManage.loginLog(openIdToken, wxOpenId, mobile, userId, sourceIp, new Date(), userToken
                , channel, deviceInfor);
        return setResultSuccess(resultJSON);
    }
}
